package batch.monitor.server;

import java.math.BigDecimal;

import javax.management.AttributeChangeNotification;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;

import batch.execution.AbstractVO;
import business.order.OrderVO;

public class BatchMng implements BatchMngMBean, NotificationEmitter {
	private int seqNum = 0;
	private int fullCount = 0;
	private int currentLoopCount = 0;
	private String batchDescription = "";
	private String batchCfgName = "";
	
	private long maxMemory = 0;
	private long totalMemory = 0;
	private long freeMemory = 0;
	
	private String companyOfOrderInfo = "";
	private String itemOforderInfo = "";
	private String personOforderInfo = "";
	private BigDecimal quantityOforderInfo = BigDecimal.ZERO;
	

	@Override
	public int getBatchFullCount() {
		return fullCount;
	}

	@Override
	public int getBatchCurrentLoopCount() {
		return currentLoopCount;
	}

	@Override
	public String getBatchDescription() {
		return batchDescription;
	}

	@Override
	public String getBatchCfgName() {
		return batchCfgName;
	}


	@Override
	public String getCompany() {
		return companyOfOrderInfo;
	}

	@Override
	public String getItem() {
		return itemOforderInfo;
	}

	@Override
	public String getPerson() {
		return personOforderInfo;
	}

	@Override
	public BigDecimal getQuantity() {
		return quantityOforderInfo;
	}

	@Override
	public long getMaxMemory() {
		return maxMemory;
	}

	@Override
	public long getTotalMemory() {
		return totalMemory;
	}
	
	@Override
	public long getFreeMemory() {
		return freeMemory;
	}


	private final NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
	
	@Override
	public void addNotificationListener(NotificationListener listener,
			NotificationFilter filter, Object handback)
			throws IllegalArgumentException {
		broadcaster.addNotificationListener(listener, filter, handback);
	}

	@Override
	public MBeanNotificationInfo[] getNotificationInfo() {
		return new MBeanNotificationInfo[] {
	               new MBeanNotificationInfo(new String[] {
	                      AttributeChangeNotification.ATTRIBUTE_CHANGE},
	                      javax.management.Notification.class.getName(),
	                      "Batch Manager stats")
	                };
	}

	@Override
	public void removeNotificationListener(NotificationListener listener)
			throws ListenerNotFoundException {
		broadcaster.removeNotificationListener(listener);
	}

	@Override
	public void removeNotificationListener(NotificationListener listener,
			NotificationFilter filter, Object handback)
			throws ListenerNotFoundException {
		broadcaster.removeNotificationListener(listener, filter, handback);
	}
	
	public void startManage(int fullSize, String batchDescription, String batchCfgName) {
		this.fullCount = fullSize;
		this.batchDescription = batchDescription;
		this.batchCfgName = batchCfgName;
		this.currentLoopCount = 0;
		this.freeMemory = Runtime.getRuntime().freeMemory();
		this.maxMemory = Runtime.getRuntime().maxMemory();
		this.totalMemory = Runtime.getRuntime().totalMemory();
	}
	
	public void nextManage() {
		nextManage(null);
	}
    public void nextManage(AbstractVO vo) {
		this.freeMemory = Runtime.getRuntime().freeMemory();
		this.maxMemory = Runtime.getRuntime().maxMemory();
		this.totalMemory = Runtime.getRuntime().totalMemory();
		
    	if( vo != null && vo instanceof OrderVO) {
    		OrderVO ovo = (OrderVO) vo;
	    	companyOfOrderInfo = ovo.getCompany();
	    	itemOforderInfo = ovo.getItem();
	    	personOforderInfo = ovo.getPerson();
	    	quantityOforderInfo = ovo.getQuantity();
    	}
    	
    	// Change Information
    	++currentLoopCount;
    	
        // Create a JMX Notification
        Notification notification = new Notification(AttributeChangeNotification.ATTRIBUTE_CHANGE,
                this, nextSeqNum(), "batchMng");
        
        // Send a JMX notification.
        broadcaster.sendNotification(notification);
    }

	private synchronized long nextSeqNum() {
		return ++seqNum;
	}
    
}
